/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.AreaDAO;
import com.cashmate.hibernate.dao.BorrowerDAO;
import com.cashmate.hibernate.dao.CollectionsDAO;
import com.cashmate.hibernate.dao.DocumentDAO;
import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.dao.PaymentsDAO;
import com.cashmate.hibernate.pojo.Areas;
import com.cashmate.hibernate.pojo.Borrowers;
import com.cashmate.hibernate.pojo.Collections;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Loans;
import com.cashmate.hibernate.pojo.Payments;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Sarah
 */
public class ViewCollection extends ActionSupport {

    private String docnumcoll, datecoll;
    public String acodecoll, acode;
    private List<Borrowers> borrowers;
    private List<String> borlist;
    private AreaDAO ar = (AreaDAO) ServletActionContext.getServletContext().getAttribute("areaDAO");
    private BorrowerDAO br = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private CollectionsDAO colldao = (CollectionsDAO) ServletActionContext.getServletContext().getAttribute("collectionsDAO");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");
    private PaymentsDAO paydao = (PaymentsDAO) ServletActionContext.getServletContext().getAttribute("paymentsDAO");
    private DocumentDAO docdao = (DocumentDAO) ServletActionContext.getServletContext().getAttribute("documentDAO");
    private EmployeeDAO empdao = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private LoansDAO loandao = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private BorrowerDAO bordao = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private Employees emp;
    private Areas area;
    private String loc;
    private String view;
    private String statprevious;
    private Collections coll;
    private List<Payments> paylist;
    private Documents document;
    private Employees employee;
    private Areas areahist;
    private List<Loans> loans;
    private List<Borrowers> borrower;

    public Areas getAreahist() {
        return areahist;
    }

    public List<Borrowers> getBorrower() {
        return borrower;
    }

    public Employees getEmployee() {
        return employee;
    }

    public List<Loans> getLoans() {
        return loans;
    }

    public Documents getDocument() {
        return document;
    }

    public List<Payments> getPaylist() {
        return paylist;
    }

    public Collections getColl() {
        return coll;
    }

    public void setColl(Collections coll) {
        this.coll = coll;
    }

    public String getStatprevious() {
        return statprevious;
    }

    public void setStatprevious(String statprevious) {
        this.statprevious = statprevious;
    }

    public String getView() {
        return view;
    }

    public void setView(String view) {
        this.view = view;
    }

    public String getLoc() {
        return loc;
    }

    public void setAcode(String acodecoll) {
        this.acodecoll = acodecoll;
    }

    public void setAcodeColl(String acode) {
        this.acode = acode;
    }

    public void setDatecoll(String datecoll) {
        this.datecoll = datecoll;
    }

    public void setDocnumcoll(String docnumcoll) {
        this.docnumcoll = docnumcoll;
    }

    public List<String> getBorlist() {
        return borlist;
    }

    public Employees getEmp() {
        return emp;
    }

    public List<Borrowers> getBorrowers() {
        return borrowers;
    }

    public Areas getArea() {
        return area;
    }

    @Action(value = "/view-coll", results = {
        @Result(name = "input", location = "${loc}"),
        @Result(name = "success", location = "${loc}")
    })
    @Override
    public String execute() {


        try {
            coll = colldao.getByAreaCodeAndDate(acodecoll, datecoll);
            if (coll == null) {     //pag wala pang existing collection sa date and area na yun
                loc = "../../loancollection.jsp?cat=coll";
                System.out.println("napunta sa coll");
                borrowers = br.getBorrowerPerAreaList(ar.getByAreaCode(acodecoll).getAreaid());
                borlist = new ArrayList<String>();
                area = ar.getAreasMap().get(acodecoll);
                emp = ar.getAreasMap().get(acodecoll).getEmployees();

                for (Borrowers bor : borrowers) {
                    borlist.add(bor.getLastname() + ", " + bor.getFirstname()); //inalis ko middlename
                }
                ServletActionContext.getRequest().setAttribute("borrowers", borrowers);
                ServletActionContext.getRequest().setAttribute("borlist", borlist);
                ServletActionContext.getRequest().setAttribute("area", area);
                ServletActionContext.getRequest().setAttribute("emp", emp);
                ServletActionContext.getRequest().setAttribute("docnumcoll", docnumcoll);
                ServletActionContext.getRequest().setAttribute("datecoll", datecoll);
            } else {        //pag meron nang existing collection para sa date and area na yun
                System.out.println("napunta sa history");
                loans = new ArrayList<Loans>();
                borrower = new ArrayList<Borrowers>();
                paylist = new ArrayList<Payments>();

                List<Payments> pym = paydao.getByCollectionID(coll);

                for (int i = 0; i < pym.size(); i++) {
                    paylist.add(pym.get(i));
                    loans.add(i, loandao.get(pym.get(i).getLoans().getLoanid()));
                    borrower.add(i, bordao.get(loans.get(i).getBorrowers().getBorrowerid()));
                }

                areahist = coll.getAreas();
                document = docdao.get(coll.getDocuments().getDocumentid());
                employee = empdao.get(coll.getAreas().getEmployees().getEmployeeid());
                ServletActionContext.getRequest().setAttribute("collection", coll);
                ServletActionContext.getRequest().setAttribute("paylist", paylist);
                ServletActionContext.getRequest().setAttribute("document", document);
                ServletActionContext.getRequest().setAttribute("employee", employee);
                ServletActionContext.getRequest().setAttribute("borrower", borrower);
                ServletActionContext.getRequest().setAttribute("area", areahist);
                statprevious = "yes";
                
                if(coll.getStat().equals("saved")){
                    loc = "../../loancollection.jsp?cat=savedcoll";
                }else if(coll.getStat().equals("posted")){
                    loc = "../../loancollection.jsp?cat=history";
                }
                

            }
            view = "yes";

            return SUCCESS;

        } catch (Exception e) {
            System.out.println("viewcollectionerr:" + e.toString());
            e.printStackTrace();
            loc = "../../loancollection.jsp?cat=coll";
        }
        return SUCCESS;
    }
}
